﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;

using FRL.DebtChased.Types;
using FRL.DebtChased.WebServices;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Declarartion of TransportSupportForWebsite class
	/// </summary>
	public static class TransportSupportForWebsite
	{

		/////////////////////////////////////////////////////////////
		#region Statc Operations - Account Plan

		public static string ComposePlanDescription(AccountPlanTransport accountPlanTransport)
		{
#if DEBUG
			Debug.Assert(accountPlanTransport.Payments.Count > 0);
#endif // #if DEBUG

			// Compose description
			StringBuilder sb = new StringBuilder();
			if (accountPlanTransport.Payments.Count > 1)
				sb.AppendFormat("{0} payments, starting on {1} with first payment of {2}, ending on {3}.",
					accountPlanTransport.Payments.Count,
					accountPlanTransport.Payments[0].ExpectedAt.ToString(Definitions.ShortDateFormat),
					new Money(accountPlanTransport.Payments[0].Amount, accountPlanTransport.Payments[0].Currency).Format(true),
					accountPlanTransport.Payments[accountPlanTransport.Payments.Count - 1].ExpectedAt.ToString(Definitions.ShortDateFormat)
				);
			else
				sb.AppendFormat("Single payment on {0} with payment of {1}.",
					accountPlanTransport.Payments.Count,
					accountPlanTransport.Payments[0].ExpectedAt.ToString(Definitions.ShortDateFormat),
					new Money(accountPlanTransport.Payments[0].Amount, accountPlanTransport.Payments[0].Currency).Format(true)
				);

			// Return variable
			return sb.ToString();

		}

		#endregion Statc Operations - Account Plan

		/////////////////////////////////////////////////////////////
		// Static operations

		public static List<string> StripAllAddresses( List<ContactTransport> contacts )
		{

			// Declare return variable
			List<string> Ftmp = new List<string>();

			// Loop through contacts
			foreach(ContactTransport ct in contacts)
			{

				// Loop through addresses
				foreach(ContactAddressTransport cat in ct.AddressList)
				{

					// Convert address to single line
					string strAddress = _addressToString(cat);
					if (!Ftmp.Contains(strAddress))
						Ftmp.Add(strAddress);

				}

			}

			// Return variable
			return Ftmp;

		}

		public static List<string> StripAllPoints(List<ContactTransport> contacts, Definitions.eContactPointType pointType)
		{

			// Declare return variable
			List<string> Ftmp = new List<string>();

			// Loop through contacts
			foreach (ContactTransport ct in contacts)
			{

				// Loop through points
				foreach (ContactPointTransport cpt in ct.PointList)
				{

					// If type matches...
					if (cpt.ContactPointType == (int)pointType)
					{

						// Add to string list
						if (!Ftmp.Contains(cpt.Address))
							Ftmp.Add(cpt.Address);

					}

				}

			}

			// Return variable
			return Ftmp;

		}

		private static string _addressToString(ContactAddressTransport address)
		{

			// Build a string
			StringBuilder sb = new StringBuilder();

			// Convert address to list of strings
			List<string> stringList = _addressToStringList(address);
			for (int i1 = 0; i1 < stringList.Count; i1++)
			{
				if (i1 > 0)
					sb.Append(", ");
				sb.Append(stringList[i1]);
			}

			// Return string
			return sb.ToString();

		}

		private static List<string> _addressToStringList(ContactAddressTransport address)
		{

			// Declare return variable
			List<string> Ftmp = new List<string>();

			// Load string array
			if (!string.IsNullOrEmpty(address.AddressLineA))
				Ftmp.Add(address.AddressLineA);
			if (!string.IsNullOrEmpty(address.AddressLineB))
				Ftmp.Add(address.AddressLineB);
			if (!string.IsNullOrEmpty(address.AddressLineC))
				Ftmp.Add(address.AddressLineC);
			if (!string.IsNullOrEmpty(address.City))
				Ftmp.Add(address.City);
			if (!string.IsNullOrEmpty(address.County))
				Ftmp.Add(address.County);
			if (!string.IsNullOrEmpty(address.Postcode))
				Ftmp.Add(address.Postcode);
			if (!string.IsNullOrEmpty(address.CountryCode))
			{

				if (CountryCodes.Dictionary.ContainsKey(address.CountryCode))
					Ftmp.Add(CountryCodes.Dictionary[address.CountryCode]);
				else
					Ftmp.Add(address.CountryCode);
			}

			// Return string
			return Ftmp;

		}

	}

}
